<%include ../includes/header.html %>
<style type="text/css">
    div#canvas-frame {
        border: none;
        cursor: pointer;
        width: 100%;
        height: 500px;
        background-color: #EEEEEE;
        position: relative;
    }

</style>
    <section class="content-header">
      <h1>
        初级教程
        <small>第5章 五彩的光源</small>
      </h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
        <li class="active">Junior</li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">
        <div id="canvas-frame"></div>
    </section>
    <!-- /.content -->
<%include ../includes/footer.html %>
<script>
  $(function(){
    var renderer;
    function initThree() {
        width = document.getElementById('canvas-frame').clientWidth;
        height = document.getElementById('canvas-frame').clientHeight;
        renderer = new THREE.WebGLRenderer({
            antialias : true
        });
        renderer.setSize(width, height);
        document.getElementById('canvas-frame').appendChild(renderer.domElement);
        renderer.setClearColor(0xFFFFFF, 1.0);
    }

    var camera;
    function initCamera() {
        camera = new THREE.PerspectiveCamera(45, width / height, 1, 10000);
        camera.position.x = 600;
        camera.position.y = 0;
        camera.position.z = 600;
        camera.up.x = 0;
        camera.up.y = 1;
        camera.up.z = 0;
        camera.lookAt(0,0,0);
    }

    var scene;
    function initScene() {
        scene = new THREE.Scene();
    }

    var light;
    function initLight() {

        // // 环境光
        // light = new THREE.AmbientLight(0x00FF00);
        // light.position.set(100, 100, 200);
        // scene.add(light)

        // // 方向光，第二个参数是光源强度，0~1之间
        // light = new THREE.DirectionalLight(0xFF0000, 1);
        // // 位置不同，方向光作用于物体的面也不同，看到的物体各个面的颜色也不一样
        // light.position.set(0,0,1);
        // scene.add(light);

        light = new THREE.PointLight(0xFF0000);
        light.position.set(0, 0, 300);
        scene.add(light)
    }

    var cube;
    var meshes = [];
    function initObject() {
        var geometry = new THREE.CubeGeometry( 200, 100, 50, 4, 4);
        var material = new THREE.MeshLambertMaterial( { color:0xFFFFFF} );
        var positions = [[0,0,0],[-300,0,0],[0,-150,0],[0,150,0],[300,0,0],[0,0,-100]];
        for(var i = 0;i<positions.length;i++) {
            var mesh = new THREE.Mesh( geometry, material);
            meshes.push(mesh);
            mesh.position.set(...positions[i]);
            scene.add(mesh);
        }
    }

    // 动画引擎
    function initTween() {
        for(var i=0;i<meshes.length;i++) {
            new TWEEN.Tween( meshes[i].position) // 移动的物体
                .to( { z: 400}, 5500 ).repeat( Infinity ).start();
        }
    }


    function threeStart() {
        initThree();
        initCamera();
        initScene();
        initLight();
        initObject();
        initTween();
        animation();
    }

    function animation()
    {
        // 改变相机位置实现动画效果
        // camera.position.x =camera.position.x +1;
        // 改变物体位置实现动画效果
        // mesh.position.x +=1
        // 渲染
        renderer.render(scene, camera);
        requestAnimationFrame(animation);
        // 使用tween动画引擎实现动画效果
        TWEEN.update();
    }

    threeStart();
  })
</script>
